﻿using System;
using System.Runtime.InteropServices;

namespace 浏览器客户端.Helper
{
    /// <summary>
    /// 设置本地时间
    /// </summary>
    public class SetTime
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct SystemTime
        {
            public short wYear;
            public short wMonth;
            public short wDayOfWeek;
            public short wDay;
            public short wHour;
            public short wMinute;
            public short wSecond;
            public short wMilliseconds;
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool SetLocalTime(ref SystemTime st);

        public static bool SetLocalTime(DateTime date)
        {
            bool flag;
            SystemTime sysTime = new SystemTime
            {
                wYear = Convert.ToInt16(date.Year),
                wMonth = Convert.ToInt16(date.Month),
                wDay = Convert.ToInt16(date.Day),
                wHour = Convert.ToInt16(date.Hour),
                wMinute = Convert.ToInt16(date.Minute),
                wSecond = Convert.ToInt16(date.Second)
            };
            //must be short           
            try
            {
                flag = SetLocalTime(ref sysTime);
            }
            catch
            {
                return false;
            }
            return flag;
        }
    }
}
